import { BoutModuleC } from "../modules/BoutModule";
import CircleIndicator_Generate from "../ui-generate/CircleIndicator_generate";

export class CircleIndicator extends CircleIndicator_Generate{
    private md:BoutModuleC

    onStart(){
        this.canUpdate = true
        this.md = ModuleService.getModule(BoutModuleC)
    }

    onUpdate(){
        let playerPos = Player.localPlayer.character.worldTransform.position
        let distance = Vector.distance(playerPos, this.md.circleCenter)
        // 在范围内
        if(distance < this.md.circleRadius){
            this.con_target.visibility = SlateVisibility.Collapsed
            this.con_arrow.visibility = SlateVisibility.Collapsed
            return
        }
        // 超出范围显示指示器
        this.con_target.visibility = SlateVisibility.SelfHitTestInvisible
        // 距离显示
        this.txt_distance.text = `${Math.ceil((distance - this.md.circleRadius) * 0.1)}米`
        this.txt_distance2.text = `${Math.ceil((distance - this.md.circleRadius) * 0.1)}米`
        // 终点图标位置转换
        let posResult = InputUtil.projectWorldPositionToWidgetPosition(this.md.circleCenter)
        // 超出屏幕
        if(posResult.screenPosition.x == 0 && posResult.screenPosition.y == 0){
            // 计算在左边还是在右边
            let cameraForward = Camera.currentCamera.worldTransform.getForwardVector()
            let circleDir = Vector.subtract(this.md.circleCenter, playerPos).normalized
            let angle = Vector2.signAngle(cameraForward, circleDir)
            // 出现在左边
            if(angle < 0){
                this.con_arrow.position = new Vector2(0, this.rootCanvas.size.y * 0.5)
                this.img_arrow.renderTransformAngle = 180
            }
            // 出现在右边
            else{
                this.con_arrow.position = new Vector2(this.rootCanvas.size.x - this.img_arrow.size.x, this.rootCanvas.size.y * 0.5)
                this.img_arrow.renderTransformAngle = 0
            }
            // 显示箭头
            this.con_arrow.visibility = SlateVisibility.SelfHitTestInvisible
            // 隐藏坐标点
            this.con_target.visibility = SlateVisibility.Collapsed
        }
        else{
            // 隐藏箭头
            this.con_arrow.visibility = SlateVisibility.Collapsed
            // 显示坐标点
            this.con_target.visibility = SlateVisibility.SelfHitTestInvisible
        }
        
        let targetPosition = Vector2.subtract(posResult.screenPosition, Vector2.multiply(this.con_target.size, 0.5))
        this.con_target.position = targetPosition
    }
}